local shilve = fk.CreateSkill {

  name = "joy_mou__shilve",

  tags = {  },

}



shilve:addEffect(fk.CardUsing, {
  name = "joy_mou__shilve",
  anim_type = "offensive",
  events = {fk.CardUsing, fk.CardResponding},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(shilve.name) and (data.card.trueName == "slash" or data.card.name == "jink" ) and
      table.find(data.card.skillNames, function(name) return string.find(name, "joy_mou__longdan") end) then
      local id
      if event == fk.CardUsing then
        if data.card.trueName == "slash" then
          id = data.tos[1][1]
        elseif data.card.name == "jink" then
          if data.responseToEvent then
            id = data.responseToEvent.from  --jink
          end
        end
      elseif event == fk.CardResponding then
        if data.responseToEvent then
          if data.responseToEvent.from == player.id then
            id = data.responseToEvent.to 
          else
            id = data.responseToEvent.from 
          end
        end
      end
      if id ~= nil then
        event:setCostData(self, id)
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, shilve.name, nil, "#joy_mou__shilve-invoke::"..event:getCostData(self))
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self))
    local choices = U.doStrategy(room, player, to, {"joy__shilve-yanqi","joy__shilve-danzhuang"}, {"joy__shilve-yanqi2","joy__shilve-danzhuang2"}, shilve.name, 1)
    local win = true
    if choices[1] == "joy__shilve-yanqi" and choices[2] ~= "joy__shilve-yanqi2" then
      player:broadcastSkillInvoke("joy_mou__shilve", 2)
      local cards = room:getCardsFromPileByRule(".|.|.|.|.|^basic")
      if #cards > 0 and not player.dead then
        local get = cards[1]
        room:obtainCard(player, get, false, fk.ReasonDraw)
      end
    elseif choices[1] == "joy__shilve-danzhuang" and choices[2] ~= "joy__shilve-danzhuang2" and player:getMark("@joy_mou__longdan") < 3 then
      player:broadcastSkillInvoke("joy_mou__shilve", 3)
      room:addPlayerMark(player,"@joy_mou__longdan",1)
    else
      win = false
      player:broadcastSkillInvoke("joy_mou__shilve", 4)
    end
  end,
})
shilve:addEffect(fk.CardResponding, {
  name = "joy_mou__shilve",
  anim_type = "offensive",
  events = {fk.CardUsing, fk.CardResponding},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(shilve.name) and (data.card.trueName == "slash" or data.card.name == "jink" ) and
      table.find(data.card.skillNames, function(name) return string.find(name, "joy_mou__longdan") end) then
      local id
      if event == fk.CardUsing then
        if data.card.trueName == "slash" then
          id = data.tos[1][1]
        elseif data.card.name == "jink" then
          if data.responseToEvent then
            id = data.responseToEvent.from  --jink
          end
        end
      elseif event == fk.CardResponding then
        if data.responseToEvent then
          if data.responseToEvent.from == player.id then
            id = data.responseToEvent.to 
          else
            id = data.responseToEvent.from 
          end
        end
      end
      if id ~= nil then
        event:setCostData(self, id)
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, shilve.name, nil, "#joy_mou__shilve-invoke::"..event:getCostData(self))
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self))
    local choices = U.doStrategy(room, player, to, {"joy__shilve-yanqi","joy__shilve-danzhuang"}, {"joy__shilve-yanqi2","joy__shilve-danzhuang2"}, shilve.name, 1)
    local win = true
    if choices[1] == "joy__shilve-yanqi" and choices[2] ~= "joy__shilve-yanqi2" then
      player:broadcastSkillInvoke("joy_mou__shilve", 2)
      local cards = room:getCardsFromPileByRule(".|.|.|.|.|^basic")
      if #cards > 0 and not player.dead then
        local get = cards[1]
        room:obtainCard(player, get, false, fk.ReasonDraw)
      end
    elseif choices[1] == "joy__shilve-danzhuang" and choices[2] ~= "joy__shilve-danzhuang2" and player:getMark("@joy_mou__longdan") < 3 then
      player:broadcastSkillInvoke("joy_mou__shilve", 3)
      room:addPlayerMark(player,"@joy_mou__longdan",1)
    else
      win = false
      player:broadcastSkillInvoke("joy_mou__shilve", 4)
    end
  end,
})

return shilve